{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Slow Rank\n",
    "集群场景通信算子快慢卡汇总分析\n",
    "\n",
    "1.根据卡粒度，统计每个Rank上的影响因子\n",
    "\n",
    "2.将统计的结果按柱状图呈现，TOP影响的极为慢卡候选  \n",
    "  \n",
    "3.展示识别出的瓶颈位置所对应的通信算子，并以箱线图形式呈现"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据准备"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "plaintext"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import plotly.offline as pyo\n",
    "\n",
    "from IPython.display import display, HTML\n",
    "\n",
    "import cluster_display\n",
    "\n",
    "display(HTML(\"<style>.container { width:100% !important; }</style>\"))\n",
    "pd.set_option('display.max_columns', None)\n",
    "pd.set_option('display.max_rows', None)\n",
    "pyo.init_notebook_mode()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 展示各Rank受影响程度的统计表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rank_stats_df = pd.read_csv(\"rank_stats.csv\", index_col=\"rankId\")\n",
    "display(rank_stats_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cluster_display.display_bar(x_axis=rank_stats_df.index, y_axes=rank_stats_df, title=\"Slow Rank\", y_index=\"slowAffectCount\")"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 展示慢卡瓶颈位置\n",
    "\n",
    "x轴按通信算子的执行顺序自左至右排列，y轴为通信算子耗时。当某个通信算子的箱线图显示其最小完成时间（min）严重偏离第一四分位数（q1）时，意味着组内耗时差异悬殊，进而表明在此次通信中大部分计算卡在等待少数慢卡。"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "slow_op_df = pd.read_csv(\"slow_op_stats.csv\", index_col=\"OpName\")\n",
    "display(slow_op_df)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "cluster_display.display_duration_boxplots_with_legend(figs=None, stats_df=slow_op_df, legend_col_name='SlowRank', x_title='Hccl OpName', y_title='Time', title='Slow Rank Bottlenecks')"
   ],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "metadata": {
     "collapsed": false
    },
    "source": []
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
